import React, {Component} from 'react';
import './index.less';
import {base, promptBox, toast} from 'nc-lightapp-front';
import {chectFormAction} from '../../../../../public/functions/checkFormAction'


class EditForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: '',
            postcontFlag: true,
            secrerycontFlag: true,
            traincontFlag: true,
        };
        this.setTableHeight = this.setTableHeight.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
        this.setCardId = this.setCardId.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
    }

    setTableHeight() {
        const mainTable = document.getElementsByClassName('main-table');
        if (mainTable[0]) {
            this.setState({
                tableHeight: mainTable[0].clientHeight - 36
            })
        }
    }

    beforeEventHandle(props, moduleId, key, value, record) {
        // 过滤协议模板信息
        let {agreementType} = this.props;
        if (key === 'pk_conttext') {
            let that = this;
            let meta = that.props.meta.getMeta();
            let pk_org = this.props.orgVal.refpk;
            meta[agreementType + 'CardAfter'].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": pk_org,
                    "GridRefActionExt": "nccloud.web.hrcm.make.sqlbuilder.ContModelGridSqlBuilder",
                    "agreementType": agreementType
                }
            };
            that.props.meta.setMeta(meta);
        }else if( key === 'pk_post'){
            let pk_dept = record['pk_psnjob.pk_dept.pk_dept'] && record['pk_psnjob.pk_dept.pk_dept'].value;
            if(!!pk_dept){
                let that = this;
                let meta = that.props.meta.getMeta();
                meta[agreementType + 'CardAfter'].items.find(item => item.attrcode === key).queryCondition = function () {
                    return {
                        "pk_dept" : pk_dept
                    }
                };
                that.props.meta.setMeta(meta);
            }
        }
        return true;
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {
        let agreementType = this.props.agreementType;
        let cardId = "agreementCard";
        let json = this.props.json;
        chectFormAction(key, cardId, props, [agreementType + 'CardAfter'], '60110503');

        // 培训协议，判断培训日期的校验
        if (agreementType === 'traincont') {
            //培训开始日期,培训结束日期
            if (key === 'trmbegindtate' || key === 'trmenddate') {
                let trmbegindtate = this.props.form.getFormItemsValue(moduleId, 'trmbegindtate').value;
                let trmenddate = this.props.form.getFormItemsValue(moduleId, 'trmenddate').value;
                if (trmbegindtate && trmenddate) {
                    //校验
                    if (new Date(trmbegindtate).getTime() > new Date(trmenddate).getTime()) {
                        promptBox({
                            color: 'info',
                            title: json['cm6011-000066'], /*警告*/
                            content: json['cm6011-000137'], /*培训结束日期必须在培训开始日期之后！*/
                            beSureBtnName: json['cm6011-000028'], /*确定*/
                            hasCloseBtn: true,
                            noFooter: false,
                            noCancelBtn: true,
                        });
                        if (key === 'trmbegindtate') {
                            this.props.form.setFormItemsValue(moduleId, {'trmbegindtate': {value: null}});
                        }
                        if (key === 'trmenddate') {
                            this.props.form.setFormItemsValue(moduleId, {'trmenddate': {value: null}});
                        }
                        return;
                    }

                }
            }
            //培训天数
            if (key === 'traindays') {
                let traindays = this.props.form.getFormItemsValue(moduleId, 'traindays').value;
                if (traindays) {
                    if (parseInt(traindays) < 0) {
                        toast({color: 'warning', content: json['cm6011-000138']}); /*培训天数不能小于0！*/
                        this.props.form.setFormItemsValue(moduleId, {'traindays': null});
                        return;
                    }
                }
            }
        }

        //协议期限和协议期限单位
        if (key === 'termmonth' || key === 'agreement_unit') {
            let num = this.props.form.getFormItemsValue(moduleId, 'termmonth').value; //协议期限
            if (parseInt(num) < 0) {
                toast({color: 'warning', content: json['cm6011-000139']}); /*协议期限不能小于0！*/
                this.props.form.setFormItemsValue(moduleId, {'termmonth': null});
                return;
            }
        }
        //协议结束日期
        if (key === 'enddate') {
            let values = this.props.form.getFormItemsValue(moduleId, 'enddate');
            let enddate = values.value;
            let begindate = this.props.form.getFormItemsValue(moduleId, 'begindate').value;
            if (begindate !== null && begindate !== '' && enddate !== null && enddate !== '') {
                //校验
                if (new Date(begindate).getTime() > new Date(enddate).getTime()) {
                    promptBox({
                        color: 'info',
                        title: json['cm6011-000066'], /*警告*/
                        content: json['cm6011-000140'], /*协议结束日期必须在协议开始日期之后！*/
                        beSureBtnName: json['cm6011-000028'], /*确定*/
                        hasCloseBtn: true,
                        noFooter: false,
                        noCancelBtn: true,
                    });
                    this.props.form.setFormItemsValue(moduleId, {'enddate': {value: null}});
                    return;
                }
            }
        }

        //试用期限和试用期限单位
        if (key === 'probmonth' || key === 'prop_unit') {
            let num1 = this.props.form.getFormItemsValue(moduleId, 'probmonth').value; //试用期限
            if (parseInt(num1) < 0) {
                toast({color: 'warning', content: json['cm6011-000141']}); /*试用期限不能小于0！*/
                this.props.form.setFormItemsValue(moduleId, {'probmonth': null});
                return;
            }

        }
        //试用结束日期
        if (key === 'probenddate') {
            let begindate = this.props.form.getFormItemsValue(moduleId, 'probegindate').value; //试用开始日期
            let enddate = this.props.form.getFormItemsValue(moduleId, 'probenddate').value; //试用结束日期

            if (begindate !== null && begindate !== '' && enddate !== null && enddate !== '') {
                if (new Date(enddate).getTime() < new Date(begindate).getTime()) {
                    promptBox({
                        color: 'info',
                        title: json['cm6011-000066'], /*警告*/
                        content: json['cm6011-000142'], /*试用结束日期必须在试用开始日期之后！*/
                        beSureBtnName: json['cm6011-000028'], /*确定*/
                        hasCloseBtn: true,
                        noFooter: false,
                        noCancelBtn: true,
                    });
                    this.props.form.setFormItemsValue(moduleId, {'probenddate': {value: null}});
                }
            }
        }
    }

    // 初始化表单数据显示
    initFormDataShow(paramObj) {

    }

    //设置表的编辑状态
    setMainFormEdit(status) {
        /*设置表单编辑状态*/
        this.props.form.setFormStatus(this.state.cardId, status);
    }

    // 调用EditForm的修改保存方法
    editInfoToSaveOrTakeEffect(val) {

    }

    // 将表格的数据重置为原来的
    resetMainFormData() {

    }

    //设置MouleId值
    setCardId(){
        this.setState({
            cardId: ''
        })
    }

    // 将表单模板key值改变
    setDataBeforeFormKey(props, paramObj) {
        this.setState({
            cardId: 'agreementCard'
        })
        // let state = this.state;
        // let flag = false;
        // let agreementType = paramObj['agreementType'];
        // let meteData = this.props.meta;
        // if(agreementType === 'traincont'){
        //     // meteData.getMeta().formrelation={traincontBaseCard: [agreementType+'CardBefore',agreementType+'CardAfter']};
        //     this.setState({
        //         cardId: 'traincontBaseCard'
        //     })
        // }else{
            // meteData.getMeta().formrelation={agreementCard: [agreementType+'CardBefore',agreementType+'CardAfter']};
            
        // }
        // 变更前后展现不同的数据
        // if (agreementType === 'postcont' && state.postcontFlag) {
        //     flag = true;
        //     this.setState({
        //         postcontFlag: false,
        //     });
        // }
        // if (agreementType === 'secrerycont' && state.secrerycontFlag) {
        //     flag = true;
        //     this.setState({
        //         secrerycontFlag: false,
        //     })

        // }
        // if (agreementType === 'traincont' && state.traincontFlag) {
        //     flag = true;
        //     this.setState({
        //         traincontFlag: false,
        //         flag
        //     })
        // }
        // flag:保证字段value只加一次‘_old’
        // if (flag) {
        //     // 改dataAfter的键名
        //     this.setState({
        //         showFlag: flag
        //     })
        //     let meta = props.meta.getMeta()[paramObj['beforeForm']];
        //     let meta2 = props.meta.getMeta()[paramObj['afterForm']];
        //     meta.items.map(obj => {
        //         obj.attrcode = obj.attrcode + '_old';
        //         switch (obj.attrcode) {
        //             case 'creator_old':
        //                 obj.visible = false;
        //                 break;
        //             case 'creationtime_old':
        //                 obj.visible = false;
        //                 break;
        //             case 'modifier_old':
        //                 obj.visible = false;
        //                 break;
        //             case 'modifiedtime_old':
        //                 obj.visible = false;
        //                 break;
        //             default:
        //                 break;
        //         }
        //     });
        //     meta2.items.map(obj => {
        //         switch (obj.attrcode) {
        //             case 'creator':
        //                 obj.visible = false;
        //                 break;
        //             case 'creationtime':
        //                 obj.visible = false;
        //                 break;
        //             case 'modifier':
        //                 obj.visible = false;
        //                 break;
        //             case 'modifiedtime':
        //                 obj.visible = false;
        //                 break;
        //             default:
        //                 break;
        //         }
        //     });
        // }
        
    }

    render() {
        const {form, orgVal, agreementType, isCardFinish} = this.props;
        return (
            <div className='main-table'>
                {this.state.cardId && this.state.cardId !=='' ? form.createForm(this.state.cardId, {
                    onBeforeEvent: this.beforeEventHandle,
                    onAfterEvent: this.afterEventHandle
                }) : ''}
            </div>
        );
    }
}

export default EditForm;
